import { message } from 'antd';
import { ModelType } from '@/common-typings';
import { riskMark } from '@/const/risk';
import {
  exportUserRiskList,
  getUserLevelList,
  getUserListData,
  modifyUserLevel,
  setUserLockStatus,
  updateUserList,
} from './service';
import { BaseLoading } from '@/components';

const CODE = {
  SUCCESS: 1,
  FAIL: 2,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const cbForRes = (fn: any = () => {}, res: any = {}) => {
  if (fn) {
    fn(res);
  }
};

const Model: ModelType = {
  namespace: 'risk',
  state: {
    // 表格区域的数据
    tableData: {
      // 外部风险查询表格数据
      userList: [],
      // 外部风险查询表格 选中表格行数据
      selectedUserList: [],
      // 外部风险查询表格列
      userListColumns: [],
      total: 0,
      pageSize: 10,
      current: 1,
    },
    // 表单区域的数据
    formData: {
      // 显示标签。控制风险类型的表格列的显示,共有17个标签，默认显示一个
      displayMarks: riskMark,
      // 外部分享查询条件
      inquiryCondition: {
        // username: '', 用户名
        // startDate: '',开始时间
        // endDate: '',结束时间
        // userRiskList: '',风险标签id用逗号连接的字符串
      },
    },
    // 修改层级模态框的数据
    modifyUserLevelModalData: {
      // 用户层级
      userLevelList: [],
      // 修改用户层级 modal选中的层级数据
      selectUserLevel: {},
    },
    // 更新
    updateInquiry: {
      // 下载失败名单的url
      updateFailUrl: '',
    },
  },
  // effects里有三个参数， put用于触发action，(put类似于dispatch,此处action跟redux的action一样)
  // call用于调用异步逻辑, fn是异步操作以后执行的回调。
  effects: {
    // 查询用户风险名单
    *getUserRiskDataEffect({ payload, fn }, { call, put }) {
      const res = yield call(getUserListData, payload);
      if (res && res.code === CODE.SUCCESS) {
        if (res.data && res.data) {
          yield put({ type: 'setUserRiskData', payload: res.data });
        }
        cbForRes(fn, res);
      }
    },
    // 导出
    *excelExportEffect({ payload, fn }, { call }) {
      const res = yield call(exportUserRiskList, payload);
      if (res.code === CODE.SUCCESS) {
        cbForRes(fn, res);
        if (res.data) {
          window.location.href = `${res.data.split('.')[0]}.xls`;
        } else {
          message.success('无可导出的数据');
        }
      }
    },
    // 更新用户信息 setUpdateFailRiskUserListUrl
    *updateRiskUserListEffect({ payload, fn }, { call, put }) {
      BaseLoading({ spinning: true });
      const res = yield call(updateUserList, payload);
      BaseLoading({ spinning: false });
      if (res && res.code === CODE.SUCCESS) {
        cbForRes(fn, res);
        if (res.data) {
          yield put({ type: 'setUpdateFailedRiskUserListUrl', payload: res.data });
        } else {
          message.success('下载失败');
        }
      }
    },
    // 设置用户的锁定状态
    *setUserLockStatusEffect({ payload, fn }, { call }) {
      const res = yield call(setUserLockStatus, payload);
      if (res.code === CODE.SUCCESS) {
        cbForRes(fn, res);
        const mes = res.data.failMessage ? res.data.failMessage : '保存成功';
        message.success(mes);
      }
    },
    // 获取修改用户层级modal的下拉框的渲染数据
    *getUserLevelListEffect({ payload }, { call, put }) {
      const res = yield call(getUserLevelList, payload);
      if (res.code === CODE.SUCCESS) {
        yield put({ type: 'setUserLevelList', payload: res.data });
      }
    },
    // 修改用户层级
    *modifyUserLevelEffect({ payload }, { call }) {
      const res = yield call(modifyUserLevel, payload);
      if (res.code === CODE.SUCCESS) {
        message.success('修改层级成功');
      }
    },
  },
  reducers: {
    // 保存查询用户风险名单的表单输入条件
    setInquiryCondition(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.formData.inquiryCondition = payload;
      return newState;
    },
    // 保存查询后的用户风险名单
    setUserRiskData(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.tableData.userList = payload.list;
      newState.tableData.total = payload.total;
      return newState;
    },
    // 风险标签，表单区操作控制表格列渲染
    changeDisplayRiskMarkColumnDisplay(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.formData.displayMarks = payload;
      return newState;
    },
    // 显示标签
    changeRiskMarkColumnDisplay(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.formData.displayMarks = payload;
      return newState;
    },
    // 用户层级
    setUserLevelList(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.modifyUserLevelModalData.userLevelList = payload;
      return newState;
    },
    // 存下载更新失败的风险名单的url
    setUpdateFailedRiskUserListUrl(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.updateInquiry.updateFailUrl = payload.detailPath;
      newState.updateInquiry.updateSuccessCount = payload.successTotal;
      return newState;
    },
    // 保存 修改用户层级 modal选中的层级数据
    setSelectUserLevel(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.modifyUserLevelModalData.selectUserLevel = payload;
      return newState;
    },
    // 外部风险查询表格 选中数据 的保存
    saveSelectedUserList(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.tableData.selectedUserList = payload;
      return newState;
    },
    // 外部风险查询表格列
    setUserListColumns(state, { payload }) {
      const newState = Object.assign({}, state);
      newState.tableData.userListColumns = payload;
      return newState;
    },
    // 清空表格的选中数据
    clearSelectedTableColumns(state) {
      const newState = Object.assign({}, state);
      newState.tableData.selectedUserList = [];
      return newState;
    },
    // 点击翻页修改表格每一页显示页码
    setPageSize(state, { payload }) {
      const newState = Object.assign({}, state);
      const { pageSize, current } = payload;
      if (pageSize) newState.tableData.pageSize = pageSize;
      if (current) newState.tableData.current = current;
      return newState;
    },
  },
};

export default Model;
